//
//  RecordEditSpectrumView.m
//  BDD_TWO
//
//  Created by XTW on 2023/6/9.
//

#import "RecordEditSpectrumView.h"
#import "EZAudioFile.h"
#import "FileManager.h"
#import "LocalRecordModel.h"

@interface RecordEditSpectrumView()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *tagImageView;
@property (weak, nonatomic) IBOutlet UILabel *tagLabel;
@property (weak, nonatomic) IBOutlet UIView *bgView;

@end

@implementation RecordEditSpectrumView

- (instancetype)initCustomView:(CGRect)frame index:(NSInteger)index recordModel:(LocalRecordModel *)model {
    self = [[[NSBundle mainBundle] loadNibNamed:@"RecordEditSpectrumView" owner:self options:nil] lastObject];
    if (self) {
        [self setFrame:frame];
        [self.bgView setBackgroundColor:ViewBackgroundColor];
        self.model = model;
        if (index == 0) {
            self.tagImageView.hidden = YES;
            self.tagLabel.hidden = YES;
        } else {
            self.tagImageView.hidden = NO;
            self.tagLabel.hidden = NO;
            self.tagLabel.text = [NSString stringWithFormat:@"%ld", index];
            self.bgView.layer.masksToBounds = YES;
        }
        if (model.needSaveUrl) {
            self.audioFile = [EZAudioFile audioFileWithURL:model.needSaveUrl];
        } else {
            NSString *path = [[FileManager sharedManager] getLocalFilePath:model.fileName];
            if ([[FileManager sharedManager] judgeObjectExistence:path] && [[NSData dataWithContentsOfFile:path] length] > 0) {
                self.audioFile = [EZAudioFile audioFileWithURL:[NSURL fileURLWithPath:path]];
            } else {
                self.audioFile = nil;
            }
        }
        
        [self refreshTime:self.audioFile.formattedDuration];
    }
    return self;
}

- (void)refreshTime:(NSString *)time {
    self.timeLabel.text = [NSString stringWithFormat:@"时长：%@", time];
}

- (void)refreshTag:(NSInteger)index {
    self.tagLabel.text = [NSString stringWithFormat:@"%ld", index];
}

- (IBAction)clickPress {
    if (!self.tagLabel.hidden) {
        [self setSelected];
        if (self.OnClickHandle) {
            self.OnClickHandle(self);
        }
    }
}

- (void)setSelected {
    self.bgView.layer.borderWidth = 1.0f;
    self.bgView.layer.borderColor = rgba(236, 94, 101, 1).CGColor;
    
}

- (void)cancelSelected {
    self.bgView.layer.borderWidth = 0;
}
@end
